package com.traffic.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.traffic.annotation.ApiJsonObject;
import com.traffic.annotation.ApiJsonProperty;
import com.traffic.annotation.Power;
import com.traffic.constant.Const;
import com.traffic.constant.Status;

import com.traffic.entity.Param;
import com.traffic.model.ApiResponse;
import com.traffic.service.ParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "公式参数相关接口")
@RestController
@RequestMapping(value = "/param")
@Slf4j
public class ParamController {

    @Autowired
    ParamService paramService;

    @ApiOperation(value = "【公式参数】", notes = "公式参数。得分：B0-B16。平顺度:A1-A6,C1-C6。年龄:age1-age3。速度:speed1-speed5")
    @ApiImplicitParam(paramType = "path", dataType = "String", name = "paramtype", value = "参数类型", required = true)
    @GetMapping("/getparam/{paramtype}")
    public ApiResponse getTargetparam(@PathVariable String paramtype) {

        List<Param> params = paramService.queryTargetParam(paramtype);

        return ApiResponse.ofStatus(Status.OK, params);

    }

    @ApiOperation(value = "【公式参数】", notes = "公式参数。得分：B0-B16。平顺度:a1-a6。年龄:age1-age3。速度:speed1-speed5。等级公式:level。" +
            "路面铺装：c1-c6。急加速：acc1-acc5。acclast。" +
            "急减速:nacc1-nacc5。nacclast。" +
            "急转弯角速度：turnSpeed1-turnspeed3.急转弯转弯角度:turnAngle1-turnAngle4。急转弯持续时间:turnlast。" +
            "急转弯1：turn1P1-turn1P3,turn1S1-turn1S3。急转弯2：turn2P1-turn2P3,turn2S1-turn2S3。急转弯3：turn3P1-turn3P3,turn3S1-turn3S3。")
    @ApiImplicitParam(paramType = "path", dataType = "String", name = "paramtype", value = "参数类型", required = true)
    @GetMapping("/getparam/all")
    public ApiResponse getAllparam() {

        List<Param> params = paramService.list();

        return ApiResponse.ofStatus(Status.OK, params);

    }

    @ApiOperation(value = "【修改参数】", notes = "修改参数可设置多个参数")
    @PostMapping("/updateparam")
    @ApiJsonObject(name = "param",value = {
            @ApiJsonProperty(type =String.class,key = "B2",example = "0.5" , description = "参数的名称和值可传多个"),
            @ApiJsonProperty(type =String.class,key = "c1",example = "0.1" , description = ""),
            @ApiJsonProperty(type =String.class,key = "a1",example = "0.1" , description = ""),
    })
    @Power(authority = Const.AUTH_PARAM)
    public ApiResponse UpdateParams(@RequestBody Map<String, String> data) {

        for(Map.Entry<String, String> i :data.entrySet()){

            paramService.update(new UpdateWrapper<Param>().eq("name",i.getKey()).set("value",i.getValue()));
        }

        return ApiResponse.ofStatus(Status.OK);

    }

}
